<?php

namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        if(!session('?usernameht')){
            $this->error("请您先登录",u('admin/Login/index'));exit;
        }

        if(method_exists($this,'__init')){

            $this->__init();
        }
    }

    public function store (model $model ,$data,$url,\Closure $callback=null)
    {
        $res = $model->store($data);
        if ($res['valid']=='success'&&$callback instanceof \Closure){
            $callback($res);
        }
        $this->message($res,$url);
    }
    private function message($res,$url)
    {
        if ( $res[ 'valid' ] == 'success' ) {
            $this->success( $res[ 'msg' ],$url);exit;
        } else {
            $this->error( $res[ 'msg' ],$url );exit;
        }
    }
}